package com.kkb.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class MybatisConfig {
    /**
     * 配置SqlSessionFactory
     */
    @Bean("sqlSessionFactory")
    public SqlSessionFactoryBean createSqlSessionFactoryBean(DataSource dataSource){
        //创建对象
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        //配置参数(数据源, pojo别名)
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setTypeAliasesPackage("com.kkb.pojo");
        return sqlSessionFactoryBean;
    }
    /**
     * 配置扫描sql映射⽂件与dao接⼝,并创建dao接⼝的动态代理对象 添加到IOC容器
     */
    @Bean("scannerConfigurer")
    public MapperScannerConfigurer createMapperScannerConfigurer(){
        //创建对象
        MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer();
        //配置参数(扫描dao包)
        scannerConfigurer.setBasePackage("com.kkb.dao");
        return scannerConfigurer;
    }
}